<%@ page contentType="text/html; charset=UTF-8" language="java" import="java.sql.*" errorPage="" %>
<%@ page import="com.google.appengine.api.users.*" %>
<%@ page import="javax.jdo.*" %>
<%@ page import="shumatz.*" %>
<%@ page import="java.util.*" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>GAE 実習用アプリ</title>
</head>

<body>
<%
		UserService userService = UserServiceFactory.getUserService();
		User user = userService.getCurrentUser();
		
		if (user != null){
%>
<p>こんにちは<%= user.getNickname() %>!  (ここから<a href="<%= userService.createLogoutURL(request.getRequestURI()) %>">サインアウト</a>できます)</p>
<% 
	} else {
%>
<p>こんにちは! <a href="<%= userService.createLoginURL(request.getRequestURI()) %>">サインイン</a>すると送信メッセージがサインイン名付きで表示されます。</p>
<%
	}
%>
<%
	PersistenceManager pm = PMF.get().getPersistenceManager();
	String query = "select from " + Greeting.class.getName() + " order by date";
	List<Greeting> greetings = (List<Greeting>) pm.newQuery(query).execute();
	if (greetings.isEmpty()){
	
%>
<p>The guestbook has no messages.</p>
<%
	} else {
		for (Greeting g: greetings) {
			if (g.getUserName() == null) {
%>
	<p>匿名書き込み</p>
<%
			} else {
%>
<p><strong><%= g.getUserName() %>の書込み</strong></p>
<%
			}
%>
<blockquote><%= g.getContent() %></blockquote>
<%
		}
	}
	pm.close();
%>
<form action="/sign" method="post">
<div>your name <br /><input type="text" name="yourname" /></div>
<div>message <br /><textarea name="content" rows="3" cols="60"></textarea></div>
<div><input type="submit" value="メッセージ送信" /></div>
</form>
</body>
</html>